package com.yunxin.admin.system.api.system;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunxin.admin.system.application.UserQueryService;
import com.yunxin.admin.system.infra.common.constant.SecurityConstants;
import com.yunxin.admin.system.infra.framework.resubmit.Resubmit;
import com.yunxin.admin.system.infra.framework.security.util.SecurityUtils;
import com.yunxin.admin.system.infra.framework.syslog.AutoLog;
import com.yunxin.common.result.PageResult;
import com.yunxin.common.result.Result;
import com.yunxin.core.application.admin.team.DistributeDo;
import com.yunxin.core.application.admin.team.DistributeResultDto;
import com.yunxin.core.application.admin.team.TeamTaskDistribute;
import com.yunxin.core.application.admin.team.TeamTaskDistributeCount;
import com.yunxin.core.application.command.order.OperateAllocationOrderAccountCommand;
import com.yunxin.core.application.command.order.OperateAllocationOrderRest;
import com.yunxin.core.application.command.order.OperateAllocationOrderStatusCommand;
import com.yunxin.core.application.command.order.OrderUserApplyAllocationMerchantPageCommand;
import com.yunxin.core.application.dto.order.OrderUserApplyAllocationMerchantPageConnectDTO;
import com.yunxin.core.application.dto.order.OrderUserApplyAllocationMerchantPageDTO;
import com.yunxin.core.application.service.OrderApplicationAdminService;
import com.yunxin.core.application.service.OrderQueryAdminService;
import com.yunxin.core.application.service.PageQueryCountService;
import com.yunxin.core.application.service.TeamService;
import com.yunxin.core.domain.model.team.Team;
import com.yunxin.core.utils.infra.utils.DateFormatUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Tag(name = "08.未接通列表")
@RestController
@RequestMapping("/core/v1/order/user/apply/connect")
@RequiredArgsConstructor
public class SysNoConnectController {

    private final OrderQueryAdminService orderQueryAdminService;

    private final UserQueryService userQueryService;

    private final OrderApplicationAdminService orderApplicationAdminService;

    private final PageQueryCountService pageQueryCountService;

    private final TeamService teamService;


    @Operation(summary = "未接通分页列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/page")
    public PageResult<OrderUserApplyAllocationMerchantPageDTO> getTeamPage(@Validated @RequestBody OrderUserApplyAllocationMerchantPageCommand command) {
        final Long userId = SecurityUtils.X.getUser().getUserId();
        List<Long> merchantIds = userQueryService.getUserMerchantIds(userId);
        if (CollUtil.isEmpty(merchantIds)) {
            return PageResult.success(Collections.emptyList(), 0);
        }
        command.setMerchantId(merchantIds.get(0));
        //商户用户角色只能查询自己的数据
        if (SecurityUtils.X.isSalesmanUser()||SecurityUtils.X.isTelephonerUser()) {
            command.setSysUserId(userId);
        }
        command.setStartAllocationTime(DateFormatUtils.X.getDateStr(DateFormatUtils.X.getDayLater(new Date(),5)));
        //查询未接通列表
        return orderQueryAdminService.findAllNoConnectList(command);
    }


    @Operation(summary = "已分配团队列表", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/allocatedTeamList")
    public Result<DistributeResultDto> operateAllocationOrderStatus(@Validated @RequestBody OperateAllocationOrderAccountCommand command) {
        command.setOperator(SecurityUtils.X.getUser().getUsername());
        return Result.success(orderApplicationAdminService.allocatedTeamList(command));
    }

    @Operation(summary = "重新分配", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/allocatedRest")
    public Result<DistributeResultDto> operateAllocationOrderRest(@Validated @RequestBody OperateAllocationOrderRest command) {
        command.setUserId(SecurityUtils.X.getUser().getUserId());
        command.setOperator(SecurityUtils.X.getUser().getUsername());
        return orderApplicationAdminService.operateAllocationOrderRest(command);
    }


    @Operation(summary = "分配团队数量统计", security = {@SecurityRequirement(name = SecurityConstants.TOKEN_KEY)})
    @PostMapping("/pageQueryCount")
    public PageResult<TeamTaskDistribute> pageQueryCount(@RequestBody TeamTaskDistributeCount teamTaskDistributeCount){
        //团队管理员只能看自己团队下的数据
        if (SecurityUtils.X.isBokeTeamUser()){
            Long userId = SecurityUtils.X.getUser().getUserId();
            List<Team> teams = teamService.getTeamListByUserId(userId);
            if (CollUtil.isEmpty(teams)) return PageResult.success(Collections.emptyList(),0);
            teamTaskDistributeCount.setTeamId(Long.valueOf(teams.get(0).getId()));
        }
        IPage<TeamTaskDistribute> result = pageQueryCountService.pageQueryCount(teamTaskDistributeCount);
        return PageResult.success(result.getRecords(),result.getTotal());
    }

}

